package com.wty.design.creational.singleton;

/**
 * @Author wangtengyu
 * @Create 2018-09-18-10:19
 * 同步的懒汉式，效率低
 */
public class SynchronizedSingleton {
    private static SynchronizedSingleton instance =null;

    //单例模式一定要把无参构造私有化
    private SynchronizedSingleton(){
        if (instance!=null){
            throw new RuntimeException("单例构造器禁止反射");
        }
    }
    public static synchronized SynchronizedSingleton getInstance(){
        if (instance ==null){
            instance =new SynchronizedSingleton();
        }
        return instance;
    }

//    public static  SynchronizedSingleton getInstance(){
//        synchronized (SynchronizedSingleton.class){
//            if (instance==null){
//                instance=new SynchronizedSingleton();
//            }
//        }
//        return instance;
//    }
}
